package com.abc.robot01.vivemodel

import android.util.Log
import androidx.compose.runtime.mutableIntStateOf
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.abc.robot01.activity.RenderActivity
import com.abc.robot01.activity.RenderActivity.Companion
import com.abc.robot01.activity.toJSONString
import com.abc.robot01.retrofit.RetrofitService
import com.abc.robot01.retrofit.bean.HouseType
import com.abc.robot01.retrofit.bean.HouseType2
import com.abc.robot01.retrofit.bean.RenderQueueData
import com.abc.robot01.retrofit.bean.SkyBoxData
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import java.math.BigDecimal


class RenderViewModel : ViewModel() {

    companion object {
        private const val TAG = "RenderViewModel"
    }

    // Mutable States
    val selectedOption = mutableStateOf("日间渲染")
    val userName = mutableStateOf("小二")
    val customerPhone = mutableStateOf("13012345678")
    val housePlansDataId = mutableStateOf("")
    val housePlansId = mutableStateOf("")
    val more = mutableStateOf("11")

    /**
     * 总数量
     */
    val totalQuantity = mutableStateOf("1")

    /**
     * 总金额
     */
    val totalAmount = mutableStateOf("0.00")

    val designPackageId = mutableStateOf("")
    val houseName = mutableStateOf("")

    // Lists
    var houseTypeList = mutableStateOf(emptyList<HouseType2>())

    private val _designerList = MutableStateFlow<List<HouseType>>(emptyList())
    val designerList: StateFlow<List<HouseType>> = _designerList

    private val _modeList = MutableStateFlow<List<RenderQueueData>>(emptyList())
    val modeList: StateFlow<List<RenderQueueData>> = _modeList

    val selectedSkyBoxName =  mutableStateOf("")

    private val _skyBoxList = MutableStateFlow<List<SkyBoxData>>(emptyList())
    val skyBoxList: StateFlow<List<SkyBoxData>> = _skyBoxList

    // Submitting logic to calculate fees
    fun submit() {
        modeList.value.find { it.name == selectedOption.value }?.let { renderConfig ->
            Log.e(TAG, "Rendering price config: ${renderConfig.name}, ratio: ${renderConfig.ratio}")
            totalAmount.value = getTotal(houseTypeList.value)
        }
    }

    fun load() {
        viewModelScope.launch {
            val  s = RetrofitService.instance.designRenderSkyBox(Robot.accessToken)
            _skyBoxList.value = s.data
            selectedSkyBoxName.value=s.data.component1().name
            Log.e(TAG, "获取天空盒配置: " + s.toJSONString())


            val a1 =
                RetrofitService.instance.designRenderPriceConfig(accessToken = Robot.accessToken)
            Log.e(TAG, "获取渲染价格配置: " + a1)
            a1.data.forEach {
                Log.e(TAG, "获取渲染价格配置: " + it)
            }
            _modeList.value = a1.data

//            val s = RetrofitService.instance.getSysDicByType(accessToken = Robot.accessToken  ,type = "ROOM_TYPE")
//            _houseTypeList.value+=s.data
//            Log.e(TAG, "房型:$s")
            Log.e(TAG, "housePlansDataId.value:" + housePlansDataId.value)
            Log.e(TAG, "designPackageId.value:" + designPackageId.value)
            val res = RetrofitService.instance.getRenderRooms(
                accessToken = Robot.accessToken,
                housePlansDataId = housePlansDataId.value,
                designPackageId = designPackageId.value
            )
            if (res.code == 200) {
                Log.e(TAG, "渲染房间: " + res)
                res.data.forEach {
                    Log.e(TAG, "空间: " + it)
                }

                houseTypeList.value = res.data.map {
                    HouseType2(id = it.roomId, name = it.roomTypeName, renderPrice = it.renderPrice)
                }
                totalQuantity.value = houseTypeList.value.size.toString()
                totalAmount.value = getTotal(houseTypeList.value)
            }
            RetrofitService.instance.countWaitRenderConfig(Robot.accessToken).also {
                Log.e(TAG, "获取待渲染的任务数: " + it)
                if (it.code == 200) {
                    more.value = it.data.toString()
                }
            }

        }
    }
}

fun getTotal(houseTypeList:List<HouseType2>):String{
    var sum = BigDecimal("0.00")
    houseTypeList.filter { it.selected }.forEach {
        val value = BigDecimal(it.renderPrice.toString())
        sum = sum.add(value)
    }
    Log.e(RenderActivity.TAG, "总费用: "+  sum)
 return   sum.toString()
}

